//-------------------------------------------------------------------
//	Copyright (c) 2010-2014  Zhirnov Andrey
//	This file is part of the "UX Standard Lib" project.
//	See copyright notice in "UX_STL_Ext.h".
//-------------------------------------------------------------------

#ifdef __UX_STL_UNIT_TEST__

//#define _DEFCFG_UXS_USE_OPENGL_DISABLED
#define _DEFCFG_UXS_USE_OPENGLES_DISABLED
//#define _DEFCFG_UXS_USE_OPENAL_DISABLED
//#define _DEFCFG_UXS_USE_OPENCL_DISABLED
#define _DEFCFG_UXS_USE_DIRECTX11_DISABLED


#include "../ux_stl_ext.h"

using namespace UX_STL;
using namespace UXTypesExt;
using namespace UXMath;


#ifdef _UXS_USE_OPENGL
using namespace GLShell;

#if defined( _UXS_USE_OPENAL ) and defined( _UXS_USE_OPENCL )
void GetSystemInfoTest()
{
	using namespace ALShell;
	using namespace CLShell;

	TSystemInfo			s_info;
	string				str;
	CLDevice			c_dev;
	GLDefaultRender		c_render;
	ALSoundSystem		c_soundsys;
	GLWindow			s_wnd;

	s_wnd.CreateGL( GLWindow::TDescriptorExt( CWindow::TDescriptor(), GLWindow::TGLDescriptor(), true ), "Test Window" );
	s_wnd.MakeCurrent();


	c_soundsys.Init( null, 4 );
	c_render.Init( &s_wnd );
	c_dev.Init( "", cl_device::ALL );

	TPlatform::GetSystemInfo( s_info );
	TPlatform::GetSystemInfoString( s_info, str );
	c_render.GetOpenGLInfo( str );
	c_render.GetOpenGLParams( str );
	c_dev.GetOpenCLInfo( str );
	c_dev.GetOpenCLParams( str );
	c_soundsys.GetOpenALInfo( str );
	str << "---------------";

	TPlatform::ErrorMsg( "Message", str.cstr(), e_msg_buttons::OK );

	c_render.Destroy();
}
#endif	// AL, CL


void GLWindowTest()
{
	class CWindowUpdater : public IWindowUpdater, public IWindowEventListener
	{
	protected:
		bool	bResized;

	public:
		CWindowUpdater(): bResized(true) {}

		void OnUpdate(CWindow *pWnd)
		{
			if ( bResized )
			{
				SMAN()->Viewport( irect( ivec2(0), pWnd->Descriptor().vSize.To<ivec2>() ) );
				bResized = false;
			}

			glClear( GL_COLOR_BUFFER_BIT );

			((GLWindow *)pWnd)->SwapBuffers();
		}

		void ProcessMessage(const TWindowEvent &sEvent)
		{
			if ( sEvent.eEventType == e_wnd_event::MESSAGE )
			{
				if ( sEvent.sEvent.sMsg.eMsg == TWindowEvent::TMessage::RESIZE ||
					 sEvent.sEvent.sMsg.eMsg == TWindowEvent::TMessage::MAXIMIZED )
				{
					bResized = true;
				}
			}
		}
	};

	GLWindow					s_wnd;
	CWindow::TDescriptor		s_desc;
	GLWindow::TGLDescriptor		s_gl_desc;
	GLDefaultRender				c_render;
	CWindowUpdater				c_updater;

	s_desc.vSize			= uvec2( 640, 480 );
	s_desc.vPos				= ivec2( 0, 0 );
	s_desc.eStyle			= e_wnd_style::ALLOW_SIZING;
	s_desc.uFreq			= 0;
	s_desc.bFullScreen		= false;

	s_gl_desc.eFlags		= e_glinit_flags::DEFAULT_CORE;
	s_gl_desc.hSharedRC		= null;
	s_gl_desc.uColorBits	= 32;
	s_gl_desc.uDepthBits	= 24;
	s_gl_desc.uStencilBits	= 8;
	s_gl_desc.uMSAASamples	= 16;
	s_gl_desc.uColorSamples	= 8;
	s_gl_desc.bVSync		= true;

	s_wnd.CreateGL( GLWindow::TDescriptorExt( s_desc, s_gl_desc, true ), "GL Window", TWindowClass( "TestWndClass", e_wc_style::GRAY ) );
	s_wnd.AddUpdater( &c_updater );

	s_wnd.MakeCurrent();
	glViewport( 0, 0, 640, 480 );
	glClearColor( 1.0f, 0.f, 0.f, 1.f );
	glClear( GL_COLOR_BUFFER_BIT );
	
	c_render.Init( &s_wnd );

	{
		s_wnd.Show();
		s_wnd.Loop();
	
		s_wnd.MakeCurrent();
	}

	c_render.Destroy();
	s_wnd.Destroy();
}

#endif	// _UXS_USE_OPENGL


#ifdef _UXS_USE_OPENGLES

void GLESWindowTest()
{
}

#endif	// _UXS_USE_OPENGLES



#ifdef _UXS_USE_DIRECTX11

void DXWindowTest()
{
	using namespace DXShell;

	CWindow					s_wnd;
	CWindow::TDescriptor	s_desc;
	DXSystem				c_render;

	s_desc.vSize			= uvec2( 640, 480 );
	s_desc.vPos				= ivec2( 0, 0 );
	s_desc.eStyle			= e_wnd_style::ALLOW_SIZING;
	s_desc.uFreq			= 0;
	s_desc.bFullScreen		= false;

	s_wnd.Create( s_desc, "DX Window", TWindowClass( "TestWndClass", e_wc_style::GRAY ) );
	
	c_render.Init( &s_wnd );
	c_render.Destroy();
	s_wnd.Destroy();
}

#endif	// _UXS_USE_DIRECTX11

#endif	// __UX_STL_UNIT_TEST__